<template>
	<view>
		<view class="map">
			<uni-icons type="location" size="18" color="#1ec0ff"></uni-icons>
		<navigator url="/pages/map/map?">
			<view class="text">
				{{nowCity.length >0?nowCity:'我的位置'}}
			</view>
		</navigator>
			
			
		
		</view>
	</view>
</template>

<script setup>
import { ref } from "vue";
import amapFile  from "../../utils/amap-wx.130.js";
import { onLoad } from "@dcloudio/uni-app";
import {apiGetLocation} from "../../api/apis.js";
import { mapKey } from "../../utils/common.js";
const nowCity = ref('');//地理位置

// // #ifdef MP-WEIXIN
// const mapKey = "a68de1e1e383a6ada4a8716204d804c1";
// // #endif

// // #ifndef MP-WEIXIN
// const mapKey = "7c09958ea6596dfa87078526f95829f5";
// // #endif


	
// 地图相关======================================
	const checkauthorize = () => {
		// #ifdef MP-WEIXIN
	  uni.getSetting({
	    success(res) {
	      if (!res.authSetting['scope.userLocation']) {
	        uni.authorize({
	          scope: 'scope.userLocation',
	          success: () => { //这里是用户同意授权后的回调
	            getLocation();
	          },
	          fail: () => { //这里是用户拒绝授权后的回调
	            rejectSetting()
	          }
	        })
	      } else { //用户已经授权过了
	        getLocation();
	      }
	    }
	  })
	  // #endif
	  
	  // #ifndef MP-WEIXIN
			getLocation();
	  // #endif
	}
	
	const getLocation = () => {
	  uni.getLocation({
	    type: 'wgs84',
	    success: function (res) {
	      console.log(res)
	      console.log('经度：' + res.longitude);
	      console.log('纬度：' + res.latitude);
		  let location = {
			  longitude:res.longitude,
			  latitude:res.latitude
		  }
		  
		  uni.setStorageSync('location',location);
		  
	      getAddress(res.latitude,res.longitude);
		  
	    }
	  });
	}
	
	//经纬度转换成省市区 latitude纬度，long经度
	const getAddress= async (latitude, longitude)=>{
		console.log(11111);
	  // #ifdef MP-WEIXIN
	  let myAmapFun = new amapFile.AMapWX({key:mapKey});
	  console.log(myAmapFun);
	  myAmapFun.getRegeo({
	    location: '' + longitude + ',' + latitude + '',//location的格式为'经度,纬度'
	    success: function (data) {
	      console.log("转换成省市",data);
	      let {desc,name} = data[0];
	      let city = (name || name?.length>0) ? name:"";
	      console.log(city)
	      nowCity.value=city + "(" + desc + ")";
	  		  console.log(nowCity.value);
	      uni.setStorageSync('city',nowCity.value)
	      console.log("省市区：",city)
		},
	    fail: function (info) {
	  			console.log(info);
	  			console.log(222222);
	  		}
	  })
	  
	  
	  // #endif
	  
	  // #ifndef MP-WEIXIN
	  let mapData = {
		  key:mapKey,
		  location:longitude + ',' + latitude,
		  urlWeb:1
	  }
	  let res = await apiGetLocation(mapData);
	  console.log(res);
	      if(res.status == 1){
				//逻辑处理
				nowCity.value = res.regeocode.formatted_address;
				uni.setStorageSync('city',nowCity.value)
		  }else{
				uni.showToast({
					title:res.info,
					icon:none
				})
		  }
	  // #endif
	  
	 
	}
	
	
	//用户拒绝授权后的回调
	const rejectSetting = () => {
	  var that = this;
	  uni.showModal({
	    title: '警告',
	    content: '授权失败，请打开位置消息授权',
	    success: (res) => {
	      if (res.confirm) { //去授权
	        toOpenSetting();
	      } else if (res.cancel) {//用户点击取消
	        console.log(res);
	        uni.showModal({
	          title: '提示',
	          content: '获取权限失败，将无法获取定位哦~',
	          showCancel: false,
	          success: (res) => {
	            toOpenSetting();
	          }
	        })
	      }
	    }
	  })
	}
	//打开微信设置页面
	const toOpenSetting = () => {
	  uni.openSetting({
	    success: (e) => {
	      console.log(e);
	    }
	  })
	}
	
	// 地图相关======================================
	onLoad(async(e)=>{
			
			
			checkauthorize();
			
			
	})
</script>

<style lang='scss' scoped>
	.map{
						 display: flex;
						 align-items: center;
						 
	}       
</style>